<?php

/*
*/

class Inbound_Type_model extends CI_Model
{

	private $inbound_type;
	private $inbound_types;
	private $_inbound_types;
	private $inbound_type_id;
	static $TABLE;

	function __construct($init=FALSE)
	{
		parent::__construct();
		if (TRUE == $init)
		{
			$this->init();
		}
	}

	public function init()
	{
		//载入库文件
		$this->load->library('test_database');
		$this->test_database->test();
		//载入核心类
		$this->load->database($this->test_database->get_db());
		//载入库文件
		$this->load->library('form_validation');
		//载入本地语言文件
		$this->lang->load('form_validation','chinese');
		//初始化
		$this->TABLE = 'inbound_types';
		$this->inbound_flgs = array('order_flg'=>'进货', 'return_flg'=>'退货', 'exchange_flg'=>'换货', 'other_flg'=>'其他');	//库类型标识符
	}

	//创建一个入库类型
	public function create()
	{
		if ($this->input->post('submit'))
		{
			$this->form_validation->set_rules('name', '类型', 'trim|required|min_length[2]|max_length[18]|is_unique[inbound_types.name]|xss_clean');
			$this->form_validation->set_rules('flg', '标识', 'trim|check_flg[inbound]|xss');
			if ( $this->form_validation->run() )
			{
				$form = $this->input->post();	//收集所有的提交数据
				$data = array(
					'name'			=> $form['name'],
					$form['flg']	=> 1,
					'create_date'	=> time() 
					);
				if ($this->db->insert($this->TABLE, $data))
				{
					if ($this->db->insert_id() != 0)
					{
						$this->inbound_type_id = $this->db->insert_id();
						return TRUE;
					}
					$this->error = 'SYS_ERR';
				}
				else
				{
					$this->error = 'SYS_ERR';
				}
			}
			else
			{
				$this->error = 'FORM_ERR';
			}
		}
		else
		{
			$this->error = 'FORM_ERR';
		}
		return FALSE;
	}

	//获取一个类目信息，不存在返回false;
	public function get_one($inbound_type_id, $valid=FALSE)
	{
		if ($query = $this->db->where('id', $inbound_type_id)->get($this->TABLE))
		{
			if ($valid)
			{
				return TRUE;
			}
			$this->inbound_type = $query->row_array();
			return TRUE;
		}
		else
		{
			$this->error = 'SYS_ERR';
		}
		return FALSE;
	}

	//获取所有类目
	public function get_all($assort='')
	{
		if ($query = $this->db->get($this->TABLE))
		{
			$this->inbound_types = $query->result_array();
			$this->_inbound_types = $this->inbound_types;
			$this->inbound_types($assort);
			return TRUE;
		}
		else
		{
			$this->error = 'SYS_ERR';
		}
		return FALSE;
	}

	//获得指定标识的入库类型
	public function get_by_flg($flg, $extend_flg=FALSE)
	{
		switch ($flg) {
			case 'order_flg':
			case 'return_flg':
			case 'exchange_flg':
			case 'other_flg':
				if ($extend_flg) $this->db->where($extend_flg, 1);
				if ($query = $this->db->where($flg, 1)->get($this->TABLE))
				{
					$this->inbound_types = $query->result_array();
					return TRUE;
				}
				break;
		}
		$this->error = 'SYS_ERR';
		return FALSE;
	}

	//判断各个类型
	public function is_order($id)
	{
		if (isset($this->inbound_types[$id]) && 'order_flg' == $this->inbound_types[$id])
		{
			return TRUE;
		}
		return FALSE;
	}

	public function is_return($id)
	{
		if (isset($this->inbound_types[$id]) && 'return_flg' == $this->inbound_types[$id])
		{
			return TRUE;
		}
		return FALSE;
	}

	public function is_exchange($id)
	{
		if (isset($this->inbound_types[$id]) && 'exchange_flg' == $this->inbound_types[$id])
		{
			return TRUE;
		}
		return FALSE;
	}

	//有效的标识符
	public function valid_flg($flg, $return_ids=FALSE)
	{
		if (array_key_exists($flg, $this->inbound_flgs))
		{
			if ($return_ids)
			{
				if ($query = $this->db->select('id')->where($flg, 1)->get($this->TABLE))
				{
					$ids = array();
					foreach ($query->result_array() as $each)
					{
						$ids[] = $each['id'];
					}
					return $ids;
				}
				return FALSE;
			}
			return TRUE;
		}
		return FALSE;
	}

	//禁用一个类型
	public function delete($inbound_type_id)
	{
		if ($this->get_one($inbound_type_id))
		{
			if ($this->inbound_type['fixed_flg'] != 1)
			{
				if ($query = $this->db->set('del_flg', 1)->update($this->TABLE))
				{
					return TRUE;
				}
				else
				{
					$this->error = 'SYS_ERR';
				}
			}
			else
			{
				$this->error = 'SYS_VALUE_ERR';
			}
		}
		else
		{
			$this->error = 'NO_EXSIT_ERR';
		}
		return FALSE;
	}

	//回收一个类型
	public function recover($id)
	{
		if ($this->get_one($inbound_type_id))
		{
			if ($query = $this->db->set('del_flg', 0)->update($this->TABLE))
			{
				return TRUE;
			}
			else
			{
				$this->error = 'SYS_ERR';
			}
		}
		else
		{
			$this->error = 'NO_EXSIT_ERR';
		}
		return FALSE;
	}

	//返回入库类型
	public function inbound_type()
	{
		return $this->inbound_type;
	}

	//处理数据，返回入库类型集合
	public function inbound_types($assort='')
	{
		$inbound_types = FALSE;		//新容器
		if ('ID_NAME' == $assort)
		{
			foreach ($this->inbound_types as $key=>$inbound_type)
			{
				foreach ($this->inbound_flgs as $flg=>$sys_name)
				{
					if ($inbound_type[$flg] == 1)
					{
						$inbound_types[$inbound_type['id']]['sys_name'] = $sys_name;
						break;
					}
				}
				$inbound_types[$inbound_type['id']]['name'] = $inbound_type['name'];
			}
		}
		else if ('ID_FLG' == $assort)
		{
			foreach ($this->inbound_types as $key=>$inbound_type)
			{
				foreach ($this->inbound_flgs as $flg=>$sys_name)
				{
					if (1 == $inbound_type[$flg])
					{
						$inbound_types[$inbound_type['id']] = $flg;
					}
				}
			}
		}
		//如果新容器内有内容
		if ($inbound_types)
		{
			$this->inbound_types = $inbound_types;
			unset($inbound_types);
		}
		return $this->inbound_types;
	}

	//返回原始数据
	public function _inbound_types()
	{
		return $this->_inbound_types;
	} 

	//返回id
	public function inbound_type_id()
	{
		return $this->inbound_type_id;
	}

	//返回系统标识
	public function inbound_flgs()
	{
		return $this->inbound_flgs;
	}

}